﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QL_DonDatHang
{
    public partial class frm_DonDatHang : Form
    {
        public frm_DonDatHang()
        {
            InitializeComponent();
            a = new Program_DDH();
        }

        #region ThuocTinh
        Program_DDH a;
        string strConn = @"Server=.\SQLEXPRESS; Database=QL_BANHANG_GIOCHA; Integrated Security=True";
        SqlConnection dataConnect;
        Form frm_TT;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        #region Phuongthuc
        //Thiết lập stt nhảy tự động
        private void dgvDeparts_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvDSDonHang.Rows[e.RowIndex].Cells["cl_STT"].Value = e.RowIndex + 1;
        }

        
        public void loadData()
        {
            //khai báo và khởi tạo dối tượng ràng buộc dữ liệu
            BindingSource bs = new BindingSource();

            //Lấy về dữ liệu dạng dataTable
            SqlDataAdapter da = new SqlDataAdapter("select * from QL_DONDATHANG", dataConnect);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgvDSDonHang.DataSource = dt;

            //Ràng buộc dữ liệu vào BindingSource
            bs.DataSource = dt;

        }
        //
        public void timkiem(string input)
        {
            for (int i = 0; i < dgvDSDonHang.Rows.Count; i++)
            {
                if (input == dgvDSDonHang.Rows[i].Cells["cl_khachHang"].Value.ToString())
                {
                    dgvDSDonHang.CurrentCell = dgvDSDonHang.Rows[i].Cells["cl_khachHang"];//đưa con trỏ vào ô đó
                    dgvDSDonHang.CurrentRow.Selected = true;//đưa con trỏ vào các ô trong rows chứa ô trên
                }
            }
        }
        //
        public void loadDataTimKiem(string tentimkiem)
        {
            BindingSource bs = new BindingSource();

            //Lấy về dữ liệu dạng dataTable
            SqlDataAdapter da = new SqlDataAdapter("select * from QL_DONDATHANG where TEN_KHACHHANG like '" + tentimkiem + "%'", dataConnect);
            DataTable dt = new DataTable();
            da.Fill(dt);

            //Ràng buộc dữ liệu vào BindingSource
            bs.DataSource = dt;

            dgvDSDonHang.DataSource = dt;
        }


        private void frm_DonDatHang_Load(object sender, EventArgs e)
        {
            dataConnect = new SqlConnection(strConn);
            dataConnect.Open();
            loadData();
        }

        private void tbnt_TimKiem_Click(object sender, EventArgs e)
        {
            timkiem(txts_timKiem.Text);
        }

        private void tbtn_MatHang_Click(object sender, EventArgs e)
        {
          //  Form frmMatHang = new frmMatHang();
           // frmMatHang.ShowDialog();
        }

        private void tbtn_Logout_Click(object sender, EventArgs e)
        {
            foreach (Control crt in this.Controls)
            {
                if (crt is ToolStrip)
                {
                    ((ToolStrip)crt).Enabled = false;
                }
            }
        }

        private void tbtn_DonHangMoi_Click(object sender, EventArgs e)
        {
            Form a = new Form_ThaoTac();
            a.ShowDialog();
            loadData();
        }

        private void dgvDSDonHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //this.tensanpham = Convert.ToString(dgvDSDonHang.CurrentRow.Cells["cl_khachHang"].Value);
            //Console.WriteLine(tensanpham);
        }

        private void txts_timKiem_TextChanged(object sender, EventArgs e)
        {
            loadDataTimKiem(txts_timKiem.Text);
        }

        private void tbtn_ThanhToan_Click(object sender, EventArgs e)
        {
            a = new Program_DDH();
            a.select_ThanhToan();
            a.IdDonHang = (int)dgvDSDonHang.CurrentRow.Cells["clID"].Value;
           // frm_TT = new frm_ThanhToan(a.IdDonHang);
            frm_TT.ShowDialog();
            loadData();
        }
        #endregion
        
    }
}
